package com.seeyon.apps.kk.manager;

import java.rmi.RemoteException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.HttpTransportProperties;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.locknlock.mm.ifmm004.IFMM004_EA_SOServiceStub;
import com.locknlock.mm.ifmm004.IFMM004_EA_SOServiceStub.IFMM004_EA_REQ_DT;
import com.locknlock.mm.ifmm004.IFMM004_EA_SOServiceStub.IFMM004_EA_REQ_MT;
import com.locknlock.mm.ifmm004.IFMM004_EA_SOServiceStub.IFMM004_EA_RES_MT;
import com.locknlock.mm.ifmm004.IFMM004_EA_SOServiceStub.IS_HEADER_type0;
import com.locknlock.mm.ifmm004.IFMM004_EA_SOServiceStub.Param_type0;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.kk.utils.ValueUtils;
import com.seeyon.v3x.services.form.FormFactory;
import com.seeyon.v3x.services.form.FormUtils;
import com.seeyon.v3x.services.form.bean.FormExport;

public class LockCaigouStopManager extends BaseManager{
	private static final Log log = LogFactory.getLog(LockKehuEduManager.class);
	  private FreemarkFlowManager freemarkFlowManager;
	public FreemarkFlowManager getFreemarkFlowManager() {
		return freemarkFlowManager;
	}
	public void setFreemarkFlowManager(FreemarkFlowManager freemarkFlowManager) {
		this.freemarkFlowManager = freemarkFlowManager;
	}
	
	public void addCaigouStopForm(Long id) {
		String findallsql = this.freemarkFlowManager.fltTemplateIntoString("sql/sap/sendtosapsql/FIND_ALL_CGSQFINSH.ftl",
				null);
		List<Map<String, Object>> queryForList = this.jdbcTemplate.queryForList(findallsql, id);
		for (Map<String, Object> map : queryForList) {
			try {
				this.addEaLog(map);
				this.sendToSap(map);
				this.UpdateLog(map);
				this.UpdateZt(id);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	  }
	  
	  public Map<String, Object> sendToSap(Map<String, Object> map) throws RemoteException {
		  if(!map.isEmpty()) {
			  HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();
				auth.setUsername("IFPO01");
				auth.setPassword("Passpo1$");
				IFMM004_EA_SOServiceStub stub = new IFMM004_EA_SOServiceStub();
				stub._getServiceClient().getOptions().setProperty(HTTPConstants.AUTHENTICATE, auth);
				IFMM004_EA_REQ_MT iFMM004_EA_REQ_MT0 = new IFMM004_EA_REQ_MT();
				IFMM004_EA_REQ_DT param = new IFMM004_EA_REQ_DT();
				IS_HEADER_type0[] array = new IS_HEADER_type0[map.size()];
				int num = 0;
				IS_HEADER_type0 bb = new IS_HEADER_type0();
				bb.setEBELN(MapUtils.getString(map, "采购编号"));
				bb.setIF_ID("IFMM003");
				Date ifdate = new Date();
				String ifdate1 = DateFormatUtils.format(ifdate, "yyyyMMddHHmmss");
				bb.setIFDATE(ifdate1);
				long mostSignificantBits = UUID.randomUUID().getMostSignificantBits();
				String UUID = String.valueOf(mostSignificantBits);
				bb.setSEQNO(UUID.substring(10));
				bb.setZAPP_INFO("2");
				array[num++] = bb;
				param.setIS_HEADER(bb);
				iFMM004_EA_REQ_MT0.setIFMM004_EA_REQ_MT(param);
				IFMM004_EA_RES_MT ifmm004_EA_SO = stub.IFMM004_EA_SO(iFMM004_EA_REQ_MT0);
				Param_type0 param2 = ifmm004_EA_SO.getIFMM004_EA_RES_MT().getParam();
				System.out.println(param2.getSTATUS() + param2.getMESSAGE() + param2.getEBELN());
		    
		  }
			return map;
	  }
	  public void addEaLog(Map<String, Object> map) {
	  		map.put("types", "采购申请单");//推送类型
			String selectYsj = this.freemarkFlowManager.fltTemplateIntoString("sql/sap/ealog/caigoushenhe.ftl", map);
			map.put("source", selectYsj);// 推送源数据
			map.put("agin",ValueUtils.getConfig().getLong("kk.CXCS"));//是否重新传输
			// 生成新的日志记录
			String dataXml = this.freemarkFlowManager.fltTemplateIntoString("sql/sap/ealog/EALOG.ftl", map); // memberDay可以直接给
			// 可以使用数据模板生成，这样底表事件依然有效
			FormFactory formFactory = (FormFactory) AppContext.getBean("formFactory");
			try {
				FormExport[] formExportData = FormUtils.xmlTransformFormExports(dataXml);
				for (FormExport formExport : formExportData) {
					formFactory.importBusinessFormData("admin1", "LK_SAPLOG_51", formExport, null);
				}
			} catch (Exception e){
				log.error(e);
			}
	  	}
	  
	  public void UpdateLog(Map<String, Object> map) {
			Map<String,Object> msp = new HashMap<>();
			msp.put("STATUS", MapUtils.getString(map, "STATUS"));
			msp.put("MESSAGER", MapUtils.getString(map, "XIAOXI"));
			msp.put("SUI", MapUtils.getString(map, "SUIJI"));
			String updatesql = this.freemarkFlowManager.fltTemplateIntoString("sql/sap/ealog/gengxin.ftl", msp);
			this.jdbcTemplate.update(updatesql);
		}
	  public void UpdateZt(Long id) {
		  	Map<String,Object> map=new HashMap<String,Object>();
		  	map.put("Formid", id);
			String updatesql = this.freemarkFlowManager.fltTemplateIntoString("sql/sap/ealog/updateZtStop.ftl", map);	
			this.jdbcTemplate.update(updatesql);
		}
}
